<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;

class Mysql extends BaseController
{

    public function index()
    {
        return $this->request->action();
    }
    public function login()
    {
        return 'hello';
    }
    //查询所有值
    public function mysql_get_all(string $input_table_name)
    {
        $user = Db::name($input_table_name)->select();
        return json($user);
    }
    //根据id查找值
    public function mysql_get_bygid(string $input_table_name, string $input_gid)
    {
        $user = Db::name($input_table_name)->where("gid", $input_gid)->findOrEmpty();
        return json($user);
    }

    //根据name查找值
    public function mysql_get_bygname(string $input_table_name, string $input_gname)
    {
        $user = Db::name($input_table_name)->where("gname", $input_gname)->findOrEmpty();
        return json($user);
    }

    //通过id更改信息
    public function mysql_insert_change_bygid(string $input_table_name, string $input_gid, string $input_gname, string $input_gtype, string $input_gnumber, string $input_gprice, string $input_gnote)
    {
        $old_gname = Db::name($input_table_name)->where("gid", $input_gid)->value("gname");
        $old_gtype = Db::name($input_table_name)->where("gid", $input_gid)->value("gtype");
        $old_gnumber = Db::name($input_table_name)->where("gid", $input_gid)->value("gnumber");
        $old_gprice = Db::name($input_table_name)->where("gid", $input_gid)->value("gprice");
        $old_gnote = Db::name($input_table_name)->where("gid", $input_gid)->value("gnote");
        if ($input_gname == "*") {
            $input_gname = $old_gname;
        }
        if ($input_gtype == "*") {
            $input_gtype = $old_gtype;
        }
        if ($input_gnumber == "*") {
            $input_gnumber = $old_gnumber;
        }
        if ($input_gprice == "*") {
            $input_gprice = $old_gprice;
        }
        if ($input_gnote == "*") {
            $input_gnote = $old_gnote;
        }
        $update_data = ['gname' => $input_gname, 'gtype' => $input_gtype, 'gnumber' => $input_gnumber, 'gprice' => $input_gprice, 'gnote' => $input_gnote];
        Db::name($input_table_name)->where("gid", $input_gid)->save($update_data);
        $user = Db::name($input_table_name)->where("gid", $input_gid)->findOrEmpty();
        return json($user);
    }

    //通过入库操作id有无均考虑
    public function mysql_insert_add_bygid(string $input_table_name, string $input_wiid, string $input_gid, string $input_gname, string $input_gtype, string $input_gnumber, string $input_gprice, string $input_gnote)
    {
        $table_name = 'warehouseinput';
        $old = Db::name($input_table_name)->where("gid", $input_gid)->findOrEmpty(false);
        if ($old) {
            $old_number = Db::name($input_table_name)->where("gid", $input_gid)->value("gnumber");
            $old_number = $old_number + (int)$input_gnumber;
            $update_data = ['gnumber' => $old_number];
            Db::name($input_table_name)->where("gid", $input_gid)->save($update_data);
            $gname = Db::name($input_table_name)->where("gid", $input_gid)->value("gname");
            $gtype = Db::name($input_table_name)->where("gid", $input_gid)->value("gtype");

            if (Db::name($table_name)->where("wiid", $input_wiid)->findOrEmpty()) {
                return 'error0';
            }

            $update_data2 = ['wiid' => $input_wiid, 'winame' => $gname, 'witype' => $gtype, 'winumber' => (int)$input_gnumber, 'wiprice' => $input_gprice, 'winote' => $input_gnote];
            Db::name($table_name)->insert($update_data2);
            // $user = Db::name($input_table_name)->where("gid", $input_gid)->findOrEmpty();
            // return json($user);
        } else {
            $update_data = ['gid' => $input_gid, 'gname' => $input_gname, 'gtype' => $input_gtype, 'gnumber' => $input_gnumber, 'gprice' => $input_gprice, 'gnote' => $input_gnote];
            Db::name($input_table_name)->insert($update_data);
            $update_data2 = ['wiid' => $input_wiid, 'winame' => $input_gname, 'witype' => $input_gtype, 'winumber' => (int)$input_gnumber, 'wiprice' => $input_gprice, 'winote' => $input_gnote];
            Db::name($table_name)->insert($update_data2);
        }
    }
    public function test(string $input_woid)
    {
        $table_name = 'warehouseoutput';
        $data = Db::name($table_name)->where("woid", $input_woid)->find();
        return $data;
    }

    //通过出库操作byid
    public function mysql_out_bygid(string $input_table_name, string $input_woid, string $input_gid, string $input_gnumber, string $input_gprice, string $input_gnote)
    {
        $table_name = 'warehouseoutput';
        $old = Db::name($input_table_name)->where("gid", $input_gid)->findOrEmpty(false);
        if ($old) {
            $old_number = Db::name($input_table_name)->where("gid", $input_gid)->value("gnumber");
            $old_number = $old_number - (int)$input_gnumber;
            if ($old_number < 0) {
                return 'error2';
            }
            if (Db::name($table_name)->where("woid", $input_woid)->find()) {
                return 'error3';
            }
            $update_data = ['gnumber' => $old_number];
            Db::name($input_table_name)->where("gid", $input_gid)->save($update_data);
            $gname = Db::name($input_table_name)->where("gid", $input_gid)->value("gname");
            $gtype = Db::name($input_table_name)->where("gid", $input_gid)->value("gtype");



            $update_data2 = ['woid' => $input_woid, 'woname' => $gname, 'wotype' => $gtype, 'wonumber' => (int)$input_gnumber, 'woprice' => $input_gprice, 'wonote' => $input_gnote];
            Db::name($table_name)->insert($update_data2);
            // $user = Db::name($input_table_name)->where("gid", $input_gid)->findOrEmpty();
            // return json($user);
        } else {
            return 'error1';
        }
    }

    //通过name
    public function mysql_insert_add_bygname(string $input_table_name, string $input_gname, string $input_number)
    {
        $old_number = Db::name($input_table_name)->where("gname", $input_gname)->value("gnumber");
        if ($old_number != null) {

            $old_number = $old_number + (int)$input_number;
            $update_data = ['gnumber' => $old_number];
            Db::name($input_table_name)->where("gname", $input_gname)->save($update_data);
            return $old_number;
        } else {
            return 'error';
        }
    }
}
